<?php
namespace App\DBs\Admin;

use Illuminate\Support\Facades\DB;
use App\DBs\BaseDB as Base;

class VideoDB extends Base
{
    //大会视频

    public function __construct()
    {
    }

    //大会视频
    public function getVideoIndex()
    {
        $res = DB::table($this->tVideo)->where('del',0)->get();
        if (!count($res)) {
            return array('code'=>-2,'msg'=>'没有数据');
        }
        $res = $this->jsonToArr($res);
        $items = [];
        foreach ($res as $re) {
            $img = env('DOMAIN').'/'.$re['img'];
            $items[] = [
                'id'=>$re['id'], 'title'=>$re['title'], 'img'=>$img, 'link'=>$re['link'],
            ];
        }
        return array('code'=>0,'msg'=>'success','data'=>$items);
    }

    //视频上传
    public function setUpload($filePath)
    {
        $code = time().rand(10000,99999);
        $totime = date('Y-m-d H:i:s',time());
        $res = DB::table($this->tVideoTmp)->insertGetId(['vcode'=>$code,'link'=>$filePath,'addtime'=>$totime]);
        if (!$res) { return ''; }
        return $code;
    }

    //视频新增
    public function setVideoAdd($arys)
    {
        $totime = date('Y-m-d H:i:s',time());
        //开启事务
        DB::beginTransaction();
        try {
            //视频新增
            $videoData = [
                'title'=>$arys['title'], 'img'=>$arys['img'],
                'link'=>$arys['link'], 'addtime'=>$totime,
            ];
            $res = DB::table($this->tVideo)->insertGetId($videoData);
            if (!$res) {
                return array('code'=>-2,'msg'=>'新增错误');
            }
            //处理临时
            $res = DB::table($this->tVideoTmp)->where('vcode',$arys['vcode'])->update(['del'=>1,'deltime'=>$totime]);
            if (!$res) {
                return array('code'=>-2,'msg'=>'临时错误');
            }
            //提交事务
            DB::commit();
            return array('code'=>0, 'msg'=>'操作成功');
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack(); echo $e->getMessage();
            return array('code'=>-1, 'msg'=>'操作失败');
        }
    }

    //视频删除
    public function setVideoDel($id)
    {
        $totime = date('Y-m-d H:i:s',time());
        $res = DB::table($this->tVideo)->where('del',0)->where('id',$id)->first();
        if (!$res) {
            return array('code'=>-2,'msg'=>'没有数据');
        }
        $res = DB::table($this->tVideo)->where('id',$id)->update(['del'=>1,'deltime'=>$totime]);
        if (!$res) {
            return array('code'=>-2,'msg'=>'删除失败');
        }
        return array('code'=>0,'msg'=>'删除成功');
    }
}